﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WoChu.RfClient.Entity;
using WoChu.RfClient.Duty;
using WoChu.RfClient.Common;
using WoChu.CF.BaseFrame.Util;

namespace WoChu.RfClient.Duty
{
    public partial class FrmBarCodeCollection : Form
    {

        public FrmBarCodeCollection()
        {
            InitializeComponent();
        }


        private static List<GoodsAllInfo> _lstGoods = null;
        public static List<GoodsAllInfo> lstGoods
        {
            get
            {
                if (_lstGoods == null) _lstGoods = new List<GoodsAllInfo>();
                return _lstGoods;
            }
            set { _lstGoods = value; }
        }
        private void FrmOffShelves_Load(object sender, EventArgs e)
        {
            txtGoodsCode.Focus(); 
        }
        //提交
        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstGoods != null && lstGoods.Count() > 0)
                {
                    if (WoChu.CF.BaseFrame.Util.MessageBoxEx.Question("是否提交？") == DialogResult.No) return;

                    Cursor.Current = Cursors.WaitCursor;
                     var  re = Duty.BarCodeCollectionUIRule.NewInstance.UpdateGoodsBarCode(lstGoods);

                    if (re.DATA)
                    {
                        lstGoods.Clear();
                        this.GoodsSource.DataSource = null;
                        this.dtGridGoods.DataSource = this.GoodsSource;
                         
                        txtGoodsCode.Text = string.Empty;
                       txtBarCode.Text = string.Empty;
                       txtGoodsCode.Focus();
                        WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("提交成功！");
                        lstGoods = null;
                    }
                    else
                    {
                        MessageBox.Show("提交失败！");
                        return;
                    }
                }
            }
 
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        //回车商品号
        private void txtGoodsCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;

            try
            {
                this.GoodsSource.DataSource = null;
                this.GoodsSource.DataSource = lstGoods;
                this.dtGridGoods.DataSource = this.GoodsSource;
                this.dtGridGoods.Refresh();
                string gbCode = txtGoodsCode.Text.Trim();
              
                if (string.IsNullOrEmpty(gbCode))
                {
                    MessageBox.Show("请扫描商品条码！");
                    txtGoodsCode.SelectAll();
                    return;
                }
                string strs0 = '0'.ToString();

                if (!gbCode.StartsWith(strs0))
                {
                    //MessageBox.Show("请扫描正确的上架0条码！");
                    txtGoodsCode.SelectAll();
                    return;
                }
                if (gbCode.Length > 10)
                    gbCode = gbCode.Substring(0, 10);
                GoodsAllInfo _Goods =Duty.BarCodeCollectionUIRule.NewInstance.GetGoodsInfoNeedCollection(gbCode).DATA;
                
                if (_Goods == null)
                {
                    MessageBox.Show("找不到该商品数据！");
                    txtGoodsCode.SelectAll();
                    return;
                }
                int curIndex = lstGoods.FindIndex(x => x.GOODSCODE == gbCode);

                if (curIndex >= 0)
                {
                    //this.dtGridGoods.UnSelect(dtGridGoods.CurrentRowIndex);
                    this.dtGridGoods.Select(curIndex);
                    this.txtBarCode.Focus();
                    return;
                }
                lstGoods.Add(_Goods);
                this.GoodsSource.DataSource = null;
                this.GoodsSource.DataSource = lstGoods;
                this.dtGridGoods.DataSource = this.GoodsSource;
                this.dtGridGoods.Refresh();
                int addIndex = lstGoods.FindIndex(x => x.GOODSCODE == gbCode);
                if (addIndex >= 0)
                    this.dtGridGoods.Select(addIndex);

                this.txtBarCode.Focus();
                this.txtBarCode.SelectAll();
 
                  
            }
            catch(Exception ex)
            {
                txtGoodsCode.SelectAll(); 
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex); 
            } 
            
        }
     
        //删除操作
        private void dtGridGoods_DoubleClick(object sender, EventArgs e)
        {
            int curIndex = dtGridGoods.CurrentRowIndex;
            if (curIndex < 0) return;
            try
            {
                string gbCode = dtGridGoods[curIndex, 1].ToString();
                GoodsAllInfo curItem = lstGoods.Find(x => x.GOODSCODE == gbCode);
  
                string gbName = curItem.GOODSNAME;

                string Message = string.Format("名称:{0}, 商品编码:{1} \n 是否删除该数据？", gbName, gbCode);
                if (MessageBoxEx.Question(Message) == DialogResult.Yes)
                {
                    lstGoods.Remove(curItem);

                    this.GoodsSource.DataSource = null;
                    this.GoodsSource.DataSource = lstGoods;
                    this.dtGridGoods.DataSource = this.GoodsSource;
                    this.dtGridGoods.Refresh();
                }
            }
            catch (Exception ex)
            {
                TraceEx.Write(ex.Message);
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("删除数据失败!");
            }
            finally
            {
                txtGoodsCode.Focus(); 
                txtGoodsCode.SelectAll();
            }
        }

        private void dtGridGoods_Click(object sender, EventArgs e)
        {
            int curIndex = dtGridGoods.CurrentRowIndex;
            if (curIndex < 0) return;

            string gbCode = dtGridGoods[curIndex, 1].ToString();
            GoodsAllInfo curItem = lstGoods.Find(x => x.GOODSCODE == gbCode);
          
            txtGoodsCode.Text = gbCode;
            txtBarCode.Text = curItem.BARCODE;
            txtBarCode.SelectAll();
            this.dtGridGoods.Select(curIndex);
        }

        private void txtBarCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter || string.IsNullOrEmpty(txtBarCode.Text)) return;

           
            string gbCode = txtGoodsCode.Text.Trim();
            if (gbCode.Length > 10)
                gbCode = gbCode.Substring(0, 10);
            int curIndex = lstGoods.FindIndex(x => x.GOODSCODE == gbCode);
            if (curIndex >= 0)
            {

                lstGoods[curIndex].BARCODE = txtBarCode.Text.Trim();
                GoodsAllInfo lastItem = lstGoods[curIndex];
                lstGoods.RemoveAt(curIndex);
                lstGoods.Add(lastItem);

                this.dtGridGoods.Refresh();

               txtBarCode.Text = string.Empty;
                txtGoodsCode.Text = string.Empty;
                txtGoodsCode.Focus();

            }
            else
            {
                txtGoodsCode.Focus();
                txtGoodsCode.SelectAll();
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("请重新扫描商品编码！");
            }
        }

        private void FrmBarCodeCollection_Closing(object sender, CancelEventArgs e)
        {
            _lstGoods = null;
        }
    }
}